package kpi.java_training.testing;

import kpi.java_training.testing.db.StatisticsManager;
import kpi.java_training.testing.education.TypeController;
import kpi.java_training.testing.questions.Question;
import kpi.java_training.testing.questions.QuestionList;
import kpi.java_training.testing.user.UserData;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Vladislav
 * Date: 14.11.13
 * Time: 17:22
 * To change this template use File | Settings | File Templates.
 */
public class LearnSystem extends Testing {
    public final int TYPE1 = 0;
    public final int TYPE2 = 1;
    public final int TYPE3 = 2;
    private ArrayList<QuestionList> questionLists;
    public LearnSystem(UserData userData) {
        super(userData);
    }
    @Override
    public void startTesting() {
        questionLists = new ArrayList<QuestionList>();
        questionLists.add(userData.getNewQuestions("type1"));
        questionLists.add(userData.getNewQuestions("type2"));
        questionLists.add(userData.getNewQuestions("type3"));
        test();
    }
    private void test() {
        output.showLine("Starting test.");
        int type = 0;
        while (!isAllTestPassed()) {
            if(!questionLists.get(type).isTestPassed()) {
                processQuestion(type);
                type++;
                if(type == 3) {
                    type = 0;
                }
            }
        }
    }
    private void processQuestion(int type) {
                String answer = askQuestion(type);
                String message = commands.executeCommand(answer);
                if (!message.equals("")) {
                    output.showLine(String.format("Command found.\n%s", message));
                } else {
                    questionLists.get(type).setQuestionAnswer( answer);
                }

    }
    @Override
    protected String askQuestion(int type) {
        switch (type){
            case TYPE1:
                output.showLine(String.format("Type: %s; \n Answer the question or type command.", questionLists.get(0).getType()));
                output.showLine(questionLists.get(0).getQuestionMessage());
                return input.getString();
            case TYPE2:
                output.showLine(String.format("Type: %s; \n Answer the question or type command.", questionLists.get(1).getType()));
                output.showLine(questionLists.get(1).getQuestionMessage());
                return input.getString();
            case TYPE3:
                output.showLine(String.format("Type: %s; \n Answer the question or type command.", questionLists.get(2).getType()));
                output.showLine(questionLists.get(2).getQuestionMessage());
                return input.getString();
        }
        output.showLine("error - wrong questionType");
        return input.getString();
    }
    private boolean isAllTestPassed() {
        if (questionLists.get(0).isTestPassed() && questionLists.get(1).isTestPassed()
                && questionLists.get(2).isTestPassed()) {
            return true;
        }
        return false;
    }
}
